package com.winit.params.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.bean.model.Bean;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.params.model.Params;
import com.winit.params.service.ParamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by wanglin on 2018/6/4.
 */
@Component
@Api(value = "参数方法", tags = {"params"}, description = "params")
@RestController
@RequestMapping("/api/v1/params")
public class ParamsController {
    @Autowired
    ParamsService paramsService;

    /**
     * 保存参数
     */
    @RequestMapping(value = "/saveParam", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存/修改参数")
    @ResponseBody
    public ExecuteResult batchSaveBean(@RequestBody Params params){
        ExecuteResult er=new ExecuteResult();
        if(StringUtils.isEmpty(params.getId())){
            er=paramsService.saveParams(params);
        }else{
            er=paramsService.updateParams(params);
        }
        return  er;
    }


    @RequestMapping(value = "/getParam", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取参数信息")
    @ResponseBody
    public ExecuteResult<Bean> getParam(@ApiParam(value = "方法关联id") @RequestParam(value = "relateId", required = false) String relateId
    ) {
        ExecuteResult executeResult=paramsService.getParams(relateId);
        return executeResult;
    }

    @RequestMapping(value = "/deleteParam", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除参数信息")
    @ResponseBody
    public ExecuteResult<Bean> deleteParam(@ApiParam(value = "方法id") @RequestParam(value = "id", required = false) String id
    ) {
        ExecuteResult executeResult=paramsService.deleteParams(id);
        return executeResult;
    }

}
